package com.dajiba.demo;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;

public class MyViewGroup extends ViewGroup {

    private int horizontalSpace = 10;
    private int verticalSpace = 10;

    public MyViewGroup(Context context) {
        super(context);
        for (int i = 0; i < 10; i++) {
            ImageView imageView = new ImageView(context);
            imageView.setLayoutParams(new LayoutParams(mChildViewWidth, mChildViewHeight));
            imageView.setBackgroundColor(Color.BLACK);
            addView(imageView);
        }
    }

    public MyViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    //子view宽高
    int mChildViewWidth = 0;
    int mChildViewHeight = mChildViewWidth;

    //上下间隔
    int mVerticalInterval = SizeUtils.dp2px(5);
    int mHorizontalInterval = SizeUtils.dp2px(5);

    //横向个数
    int mHorizontalSum = 3;

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int hadUsedHorizontal = 0;//水平已经使用的距离
        int hadUsedVertical = 0;//垂直已经使用的距离
        int width = getMeasuredWidth();
//        int height = getMeasuredHeight();
        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);
            //判断是否已经超出宽度
            if (view.getMeasuredWidth() + hadUsedHorizontal > width) {
                //已经超出了宽度
                hadUsedVertical = hadUsedVertical + view.getMeasuredHeight() + verticalSpace;
                hadUsedHorizontal = 0;
            }
            view.layout(hadUsedHorizontal, hadUsedVertical, hadUsedHorizontal + view.getMeasuredWidth(), hadUsedVertical + view.getMeasuredHeight());
            hadUsedHorizontal = hadUsedHorizontal + horizontalSpace + view.getMeasuredWidth();
        }
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);
            measureChild(view, widthMeasureSpec, heightMeasureSpec);
        }
        setMeasuredDimension(ScreenUtils.getScreenWidth(),ScreenUtils.getScreenWidth());
    }

    /**
     * @param child                   子View
     * @param parentWidthMeasureSpec  宽度测量规格
     * @param widthUsed               父view在宽度上已经使用的距离
     * @param parentHeightMeasureSpec 高度测量规格
     * @param heightUsed              父view在高度上已经使用的距离
     */
    @Override
    protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {
        super.measureChildWithMargins(child, parentWidthMeasureSpec, widthUsed, parentHeightMeasureSpec, heightUsed);
    }
}

